package com.demo2do.fms.admin.web.controller.data.business;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.data.WarehouseSearch;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.data.business.Warehouse;

/**
 * 查询起始地目的地代码
 *
 * @author Chang Liu
 * 
 */
@Controller
@RequestMapping("/warehouses")
@MenuSetting("menu-data")
public class WarehouseController {
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Warehouse> warehouseService;
    
    /**
     * 
     * @param page
     * @return
     */
    @MenuSetting("submenu-warehouse")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(Page page){
        
        ModelAndView modelAndView = new ModelAndView("data/business/warehouse/warehouse-list");
        
        int totals = warehouseService.count(Warehouse.class);
        if(totals > 0){
            
            List<Warehouse> warehouses = warehouseService.listByPage(Warehouse.class, page.with(totals));
            
            modelAndView.addObject("warehouses", warehouses);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @return
     */
    @MenuSetting("submenu-warehouse")
    @RequestMapping(value = "search", method = RequestMethod.GET)
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView("data/business/warehouse/warehouse-search");
        return modelAndView;
    }

    /**
     * 
     * @param warehouseSearch
     * @param page
     * @param request
     * @return
     */
    @MenuSetting("submenu-warehouse")
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    public ModelAndView onSearch(WarehouseSearch warehouseSearch, Page page, HttpServletRequest request){
        
        ModelAndView modelAndView = new ModelAndView("data/business/warehouse/warehouse-list");
        
        int totals = warehouseService.count(Warehouse.class, warehouseSearch.getFilter());
        if (totals > 0) {
            
            List<Warehouse> warehouses = warehouseService.search(Warehouse.class, warehouseSearch, page.with(totals));
            
            modelAndView.addObject("warehouses", warehouses);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        }
        
        return modelAndView;
    }
    
    /**
     * 输出当前页记录/输出所有记录
     * 
     * @param warehouseSearch
     * @param page
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(WarehouseSearch warehouseSearch,Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/business/warehouse-export.xlsx"));
        
        // 1. list result
        int totals = warehouseService.count(Warehouse.class, warehouseSearch.getFilter());
        if (totals > 0) {
            List<Warehouse> warehouses = warehouseService.search(Warehouse.class, warehouseSearch, page.with(totals));
            modelAndView.addObject("warehouses", warehouses);
        }
     
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
}
